{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 基于用户的协同过滤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# -*- coding:utf-8 -*-\n",
    "#import sys\n",
    "#reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")\n",
    "###以上代码适用于python2，Python3替换成如下的代码：\n",
    "import importlib\n",
    "import sys\n",
    "importlib.reload(sys)\n",
    "#sys.setdefaultencoding(\"utf-8\")  ###Python3字符串默认编码unicode, 所以sys.setdefaultencoding也不存在了\n",
    "\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import pickle\n",
    "import scipy.io as sio\n",
    "import os\n",
    "\n",
    "#距离\n",
    "import scipy.spatial.distance as ssd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 读入数据做初始化\n",
    "    \n",
    "#用户和item的索引\n",
    "user_index = pickle.load(open(\"user_index.pkl\", 'rb'))\n",
    "item_index = pickle.load(open(\"item_index.pkl\", 'rb'))\n",
    "\n",
    "n_users = len(user_index)\n",
    "n_items = len(item_index)\n",
    "    \n",
    "#用户-物品关系矩阵R\n",
    "user_item_scores = sio.mmread(\"user_items_scores\").todense()\n",
    "    \n",
    "#倒排表\n",
    "##每个用户播放的歌曲\n",
    "user_items = pickle.load(open(\"user_items.pkl\", 'rb'))\n",
    "##事件参加的用户\n",
    "item_users = pickle.load(open(\"item_users.pkl\", 'rb'))\n",
    "\n",
    "#所有item之间的相似度\n",
    "#similarity_matrix = pickle.load(open(\"items_similarity.pkl\", 'rb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算每个用户的平均打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "mu = np.zeros(n_users)\n",
    "for u in range(n_users):  \n",
    "    n_items_user = 0\n",
    "    r_acc = 0.0\n",
    "    \n",
    "    for i in user_items[u]:  #用户打过分的item\n",
    "        r_acc += user_item_scores[u,i]\n",
    "        n_items_user += 1\n",
    " \n",
    "    mu[u] = r_acc/n_items_user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.00470798,  0.01424275,  0.00961421, ...,  0.08912656,\n",
       "        0.00853275,  0.01698842])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算两个用户之间的相似度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "uid1 = 0\n",
    "uid2 = 1\n",
    "si={}  #有效item（两个用户均有打分的item）的集合\n",
    "for item in user_items[uid1]:  #uid1所有打过分的Item1\n",
    "    if item in user_items[uid2]:  #如果uid2也对该Item打过分\n",
    "        si[item]=1  #item为一个有效item\n",
    "        \n",
    "n=len(si)   #有效item数，有效item为即对uid对Item打过分，uid2也对Item打过分\n",
    "if (n!=0):  #没有共同打过分的item，相似度设为0？\n",
    "    #用户uid1打过分的所有有效的item\n",
    "    s1=np.array([user_item_scores[uid1,item] for item in si])  \n",
    "    s1 -= mu[uid1]\n",
    "        \n",
    "    #用户uid2打过分的所有有效的Item\n",
    "    s2=np.array([user_item_scores[uid2,item] for item in si])\n",
    "    s2 -= mu[uid2]\n",
    "        \n",
    "    similarity = 1 - ssd.cosine(s1, s2)\n",
    "else:\n",
    "    similarity = 0.0; "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def user_similarity(uid1, uid2 ):\n",
    "    si={}  #有效item（两个用户均有打分的item）的集合\n",
    "    for item in user_items[uid1]:  #uid1所有打过分的Item1\n",
    "        if item in user_items[uid2]:  #如果uid2也对该Item打过分\n",
    "            si[item]=1  #item为一个有效item\n",
    "        \n",
    "    n=len(si)   #有效item数，有效item为即对uid对Item打过分，uid2也对Item打过分\n",
    "    if (n==0):  #没有共同打过分的item，相似度设为0？\n",
    "        similarity=0  \n",
    "        return similarity  \n",
    "        \n",
    "    #用户uid1打过分的所有有效的item\n",
    "    s1=np.array([user_item_scores[uid1,item] for item in si])  \n",
    "        \n",
    "    #用户uid2打过分的所有有效的Item\n",
    "    s2=np.array([user_item_scores[uid2,item] for item in si])  \n",
    "        \n",
    "    similarity = 1 - ssd.cosine(s1, s2) \n",
    "    return similarity  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 计算用户对每个item的预测打分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "cur_user = '5a905f000fc1ff3df7ca807d57edb608863db05d'\n",
    "\n",
    "cur_user_id = user_index[cur_user]\n",
    "cur_user_items = user_items[cur_user_id]\n",
    "\n",
    "sim_accumulate=0.0  \n",
    "rat_acc=0.0 \n",
    "\n",
    "user_items_scores = np.zeros(n_items)\n",
    "\n",
    "for i in range(n_items):  # all items of user not played\n",
    "    if i not in cur_user_items:\n",
    "        for user in item_users[i]:  #对item i打过分的所有用户\n",
    "            #计算当前用户与给item i打过分的用户之间的相似度\n",
    "            sim = user_similarity(uid1 = user,uid2 = cur_user_id)\n",
    "            \n",
    "            if sim != 0: \n",
    "                rat_acc += sim * (user_item_scores[user,i] - mu[user])   #用户user对item i的打分\n",
    "                sim_accumulate += sim  \n",
    "        \n",
    "        if sim_accumulate != 0: #no same user rated,return average rates of the data  \n",
    "            user_items_scores[i] = rat_acc/sim_accumulate    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.01528587,  0.01124084,  0.00907394,  0.00682244,  0.00709192,\n",
       "        0.0060779 ,  0.00547098,  0.00579267,  0.0059076 ,  0.00586855,\n",
       "        0.00572071,  0.00564137,  0.00554216,  0.00503872,  0.00461568,\n",
       "        0.        ,  0.0042172 ,  0.00410428,  0.00408646,  0.00379802,\n",
       "        0.00358132,  0.00361514,  0.00336921,  0.00338263,  0.00338326,\n",
       "        0.00338291,  0.        ,  0.00328742,  0.00309619,  0.00299478,\n",
       "        0.00295593,  0.00295041,  0.        ,  0.00300609,  0.00309249,\n",
       "        0.00294465,  0.        ,  0.00284634,  0.0027061 ,  0.        ,\n",
       "        0.00270795,  0.00256368,  0.00246782,  0.00252088,  0.00255834,\n",
       "        0.00255419,  0.00255216,  0.00242695,  0.00242963,  0.00244985,\n",
       "        0.00240103,  0.00231012,  0.00221765,  0.00213839,  0.0021466 ,\n",
       "        0.00210637,  0.00211982,  0.00205662,  0.00202518,  0.00209456,\n",
       "        0.00204739,  0.00204926,  0.00204413,  0.00198183,  0.00190907,\n",
       "        0.00185695,  0.001802  ,  0.00181198,  0.        ,  0.00175175,\n",
       "        0.00170298,  0.00166592,  0.00162631,  0.        ,  0.00155251,\n",
       "        0.00151245,  0.00148242,  0.00143293,  0.00142142,  0.00137155,\n",
       "        0.00137987,  0.00134179,  0.00131997,  0.00129517,  0.00125228,\n",
       "        0.        ,  0.00122156,  0.00119437,  0.00115444,  0.00110307,\n",
       "        0.00113356,  0.00110201,  0.0010786 ,  0.001094  ,  0.0010977 ,\n",
       "        0.00108267,  0.00106165,  0.00104418,  0.00102836,  0.00099749,\n",
       "        0.00097682,  0.00098247,  0.00094524,  0.00091478,  0.        ,\n",
       "        0.00088854,  0.00086483,  0.00083894,  0.        ,  0.00085614,\n",
       "        0.00083169,  0.00081201,  0.00082201,  0.00080002,  0.00079026,\n",
       "        0.00076424,  0.00075176,  0.00076475,  0.00078241,  0.0007863 ,\n",
       "        0.00076701,  0.00077777,  0.00079918,  0.00077661,  0.00075982,\n",
       "        0.00073567,  0.00072083,  0.00072457,  0.00071509,  0.00069807,\n",
       "        0.00066731,  0.00068731,  0.00067944,  0.00066604,  0.00062609,\n",
       "        0.00063529,  0.00065928,  0.0006401 ,  0.00063936,  0.00063604,\n",
       "        0.00062554,  0.00062226,  0.00061346,  0.00059363,  0.00057959,\n",
       "        0.00056211,  0.00057166,  0.00056517,  0.00056468,  0.00055983,\n",
       "        0.00056448,  0.00057967,  0.00058704,  0.00058123,  0.00057341,\n",
       "        0.00058525,  0.00059426,  0.00057805,  0.00057458,  0.00055282,\n",
       "        0.00054183,  0.00052454,  0.00051145,  0.0004978 ,  0.000499  ,\n",
       "        0.00048271,  0.00046718,  0.00044969,  0.00045244,  0.00048052,\n",
       "        0.00046215,  0.00046342,  0.00045569,  0.00044527,  0.        ,\n",
       "        0.00045209,  0.00043543,  0.00042414,  0.00043205,  0.00042642,\n",
       "        0.0004141 ,  0.00040666,  0.00041291,  0.00042159,  0.00041978,\n",
       "        0.00040493,  0.0004082 ,  0.0004045 ,  0.00041581,  0.        ,\n",
       "        0.00042309,  0.        ,  0.00040875,  0.00042002,  0.00041519,\n",
       "        0.00041571,  0.00039951,  0.00038615,  0.00037792,  0.00038497,\n",
       "        0.00038926,  0.        ,  0.00038699,  0.00037828,  0.00036908,\n",
       "        0.00036301,  0.00035853,  0.00035203,  0.00034388,  0.0003483 ,\n",
       "        0.00034457,  0.00035497,  0.00035863,  0.00036701,  0.00037386,\n",
       "        0.00037573,  0.0003831 ,  0.00037842,  0.00038238,  0.00037986,\n",
       "        0.00038391,  0.00038933,  0.00037911,  0.00037005,  0.00035692,\n",
       "        0.00035559,  0.00035619,  0.00034869,  0.00034935,  0.00034676,\n",
       "        0.00033903,  0.00034795,  0.00034139,  0.00033123,  0.00033563,\n",
       "        0.00034756,  0.00036228,  0.        ,  0.00036313,  0.00037004,\n",
       "        0.00038325,  0.00037445,  0.00038404,  0.0003815 ,  0.00039803,\n",
       "        0.00040454,  0.00041081,  0.00040493,  0.00039508,  0.00039091,\n",
       "        0.0003885 ,  0.00038303,  0.00039489,  0.00038859,  0.00038557,\n",
       "        0.        ,  0.00037769,  0.00036839,  0.00037048,  0.00036583,\n",
       "        0.00036952,  0.00036428,  0.00036747,  0.00036212,  0.00036372,\n",
       "        0.00036493,  0.00036969,  0.00036891,  0.00036486,  0.00037113,\n",
       "        0.00038157,  0.00037165,  0.00037566,  0.00037646,  0.00037958,\n",
       "        0.0003808 ,  0.00037406,  0.00037121,  0.000366  ,  0.00036244,\n",
       "        0.00035939,  0.00036701,  0.00038193,  0.00037161,  0.00038316,\n",
       "        0.00038549,  0.00038249,  0.00039784,  0.00039266,  0.00038594,\n",
       "        0.00038196,  0.00037885,  0.00038207,  0.00037206,  0.00036999,\n",
       "        0.00037019,  0.00037556,  0.00036458,  0.0003681 ,  0.00038816,\n",
       "        0.00038227,  0.00038419,  0.00037411,  0.00037388,  0.00037558,\n",
       "        0.00037479,  0.00038375,  0.00037634,  0.00036749,  0.00036838,\n",
       "        0.0003605 ,  0.00036992,  0.00037211,  0.00037489,  0.00036659,\n",
       "        0.00036937,  0.00036273,  0.00036138,  0.00036085,  0.00035038,\n",
       "        0.00035467,  0.00035586,  0.        ,  0.00036304,  0.00037029,\n",
       "        0.00037722,  0.00037541,  0.00037876,  0.00037669,  0.00036915,\n",
       "        0.00036907,  0.00036645,  0.0003628 ,  0.0003551 ,  0.00036852,\n",
       "        0.        ,  0.00037026,  0.00036581,  0.00036766,  0.00035514,\n",
       "        0.00034961,  0.00034761,  0.00034934,  0.00034929,  0.00034687,\n",
       "        0.00034104,  0.00033703,  0.0003294 ,  0.00033277,  0.00033785,\n",
       "        0.0003404 ,  0.00033734,  0.00033795,  0.0003402 ,  0.00033608,\n",
       "        0.        ,  0.00034055,  0.00034273,  0.00034929,  0.00034422,\n",
       "        0.00034405,  0.00034267,  0.00034049,  0.0003378 ,  0.00033509,\n",
       "        0.00033681,  0.0003382 ,  0.00033328,  0.00033356,  0.00032956,\n",
       "        0.00032377,  0.00032948,  0.00033658,  0.00033617,  0.00033174,\n",
       "        0.        ,  0.00033317,  0.00033369,  0.00033584,  0.0003349 ,\n",
       "        0.00034744,  0.00035092,  0.00034506,  0.00035621,  0.00035168,\n",
       "        0.00034988,  0.0003468 ,  0.00034546,  0.00034788,  0.00034589,\n",
       "        0.00034119,  0.00033856,  0.00034338,  0.0003443 ,  0.00034179,\n",
       "        0.00034203,  0.00034073,  0.00033561,  0.00033908,  0.00034648,\n",
       "        0.        ,  0.00034439,  0.00033981,  0.00033597,  0.00034127,\n",
       "        0.00034256,  0.00034007,  0.        ,  0.00033697,  0.00033308,\n",
       "        0.00033444,  0.00033242,  0.00032926,  0.00032042,  0.00031859,\n",
       "        0.00031745,  0.00032138,  0.00032022,  0.00031727,  0.00030727,\n",
       "        0.00030646,  0.00030852,  0.00030807,  0.        ,  0.00031292,\n",
       "        0.00030991,  0.00030769,  0.00031014,  0.00031365,  0.00031383,\n",
       "        0.00030889,  0.00030425,  0.00030026,  0.00031103,  0.00031857,\n",
       "        0.00031628,  0.00031377,  0.00031761,  0.00031531,  0.00031405,\n",
       "        0.00031754,  0.0003207 ,  0.00031987,  0.0003179 ,  0.00031485,\n",
       "        0.00031783,  0.0003142 ,  0.0003114 ,  0.00030607,  0.00029883,\n",
       "        0.0002957 ,  0.        ,  0.00028975,  0.0002907 ,  0.00028836,\n",
       "        0.00029537,  0.0002948 ,  0.00029881,  0.00029839,  0.00029673,\n",
       "        0.00030438,  0.0003034 ,  0.00030221,  0.0003089 ,  0.00031329,\n",
       "        0.00031038,  0.00031021,  0.00031165,  0.00030944,  0.0003094 ,\n",
       "        0.00029961,  0.00029601,  0.00029278,  0.        ,  0.00029158,\n",
       "        0.00028591,  0.        ,  0.00029633,  0.00029597,  0.00029457,\n",
       "        0.00029469,  0.0002913 ,  0.00028875,  0.00028854,  0.00028652,\n",
       "        0.00028672,  0.00029062,  0.00029116,  0.00029263,  0.00029016,\n",
       "        0.0002897 ,  0.00028342,  0.00028139,  0.00028046,  0.00027981,\n",
       "        0.00028019,  0.00027943,  0.00028417,  0.00028936,  0.00029998,\n",
       "        0.00029458,  0.00029537,  0.00029657,  0.00030066,  0.00029836,\n",
       "        0.00029796,  0.00029551,  0.00029354,  0.00029087,  0.00028527,\n",
       "        0.00028247,  0.00027976,  0.00028001,  0.00027558,  0.00027586,\n",
       "        0.00027337,  0.        ,  0.00026541,  0.00026685,  0.        ,\n",
       "        0.00025992,  0.        ,  0.00025985,  0.        ,  0.00025768,\n",
       "        0.00025761,  0.        ,  0.00026073,  0.0002552 ,  0.00025126,\n",
       "        0.00024987,  0.00023308,  0.00023337,  0.00022864,  0.000231  ,\n",
       "        0.00022893,  0.00023132,  0.00023198,  0.00023065,  0.00022945,\n",
       "        0.00023206,  0.00022839,  0.00022806,  0.00022355,  0.00023339,\n",
       "        0.00023369,  0.00022965,  0.        ,  0.0002303 ,  0.00023742,\n",
       "        0.00024065,  0.00023962,  0.00024329,  0.00024316,  0.00024489,\n",
       "        0.0002435 ,  0.00024095,  0.00024328,  0.00023952,  0.00023735,\n",
       "        0.00023578,  0.00023781,  0.00023867,  0.00024227,  0.00023932,\n",
       "        0.00023846,  0.        ,  0.00023723,  0.00023522,  0.00023265,\n",
       "        0.00023041,  0.00023381,  0.0002302 ,  0.00023348,  0.0002339 ,\n",
       "        0.0002309 ,  0.        ,  0.00022792,  0.00022851,  0.00022974,\n",
       "        0.        ,  0.00022783,  0.00022732,  0.00022679,  0.00022439,\n",
       "        0.00022567,  0.00022265,  0.00022305,  0.        ,  0.00021921,\n",
       "        0.00021634,  0.00021618,  0.00021797,  0.00022245,  0.00022046,\n",
       "        0.00021747,  0.0002133 ,  0.00021123,  0.00021233,  0.00021156,\n",
       "        0.00020705,  0.00021136,  0.00020851,  0.00020748,  0.00020842,\n",
       "        0.00020664,  0.00020615,  0.        ,  0.00020803,  0.00020735,\n",
       "        0.00020698,  0.00020853,  0.00020526,  0.00020595,  0.00020456,\n",
       "        0.00020892,  0.00021173,  0.00021687,  0.0002193 ,  0.00022057,\n",
       "        0.00022178,  0.00022572,  0.0002228 ,  0.00022115,  0.00022008,\n",
       "        0.00022084,  0.00022197,  0.00022328,  0.00022048,  0.00022717,\n",
       "        0.00023581,  0.00023714,  0.00023234,  0.00023187,  0.00023127,\n",
       "        0.00022965,  0.00022643,  0.00022838,  0.00022905,  0.00023018,\n",
       "        0.00023325,  0.00023064,  0.0002308 ,  0.00022712,  0.00023107,\n",
       "        0.00023268,  0.000232  ,  0.00023304,  0.00023017,  0.00022776,\n",
       "        0.00022499,  0.00022209,  0.00022572,  0.00022526,  0.00022623,\n",
       "        0.00022724,  0.00022497,  0.00022738,  0.00022905,  0.00023139,\n",
       "        0.00023026,  0.00022404,  0.00022068,  0.00021711,  0.00021541,\n",
       "        0.00021491,  0.0002119 ,  0.00021031,  0.00021451,  0.00021411,\n",
       "        0.00021822,  0.00021622,  0.00021159,  0.00021006,  0.00021151,\n",
       "        0.00020761,  0.00020376,  0.        ,  0.00020789,  0.00020754,\n",
       "        0.00020433,  0.00020173,  0.00019861,  0.00019851,  0.0001963 ,\n",
       "        0.00019409,  0.00019175,  0.00018417,  0.        ,  0.00018275,\n",
       "        0.00018614,  0.00018745,  0.00018937,  0.00018964,  0.00018871,\n",
       "        0.00018826,  0.00018653,  0.00018792,  0.        ,  0.00018937,\n",
       "        0.0001879 ,  0.00018331,  0.00017952,  0.00017465,  0.00017733,\n",
       "        0.00017618,  0.00017583,  0.00017272,  0.00017323,  0.00017093,\n",
       "        0.00016912,  0.00016823,  0.00016817,  0.00016522,  0.00016153,\n",
       "        0.00015957,  0.00016095,  0.0001618 ,  0.00016416,  0.00016329,\n",
       "        0.00016076,  0.00015942,  0.00016025,  0.00016083,  0.00016685,\n",
       "        0.00016591,  0.00017827,  0.        ,  0.00017841,  0.00018033,\n",
       "        0.00017919,  0.0001806 ,  0.00017919,  0.00017758,  0.00017819,\n",
       "        0.00017549,  0.00017352,  0.0001712 ,  0.0001678 ,  0.00016934,\n",
       "        0.00016901,  0.0001683 ,  0.00016579,  0.00016412,  0.00016348,\n",
       "        0.00016081,  0.00016039,  0.        ,  0.        ,  0.00015764,\n",
       "        0.00015753,  0.0001555 ,  0.00015231,  0.00015619,  0.00015602,\n",
       "        0.00015351,  0.00014967,  0.0001493 ,  0.00014509,  0.00014177,\n",
       "        0.0001383 ,  0.00014123,  0.00014522,  0.        ,  0.00014618,\n",
       "        0.00014496,  0.00014425,  0.00014294,  0.00014166,  0.0001414 ,\n",
       "        0.00013812,  0.00013798,  0.00013764,  0.00014029,  0.00014182,\n",
       "        0.00013772,  0.0001359 ,  0.00013463,  0.00013285,  0.00013078,\n",
       "        0.00012979,  0.00013173,  0.0001301 ,  0.00012916,  0.0001303 ,\n",
       "        0.00013323,  0.0001326 ,  0.00013336,  0.00013701,  0.00013827,\n",
       "        0.00013648,  0.00013257,  0.00013003,  0.00012924,  0.00012989,\n",
       "        0.00013046,  0.00013101,  0.00012864,  0.0001288 ,  0.00013094,\n",
       "        0.00012929,  0.00013079,  0.00012927,  0.00012702,  0.00012764,\n",
       "        0.00012245,  0.00012459,  0.00012644,  0.00012755,  0.00012504,\n",
       "        0.00012321,  0.00012557,  0.00012607,  0.00012591,  0.0001257 ,\n",
       "        0.00013031,  0.00012936,  0.00012796,  0.        ,  0.00012602,\n",
       "        0.00012577,  0.00012684,  0.00012502,  0.00012469,  0.00012914,\n",
       "        0.00013024,  0.        ,  0.00013425,  0.0001338 ,  0.00013361,\n",
       "        0.00013737,  0.00013698,  0.00013494,  0.00013494,  0.        ,\n",
       "        0.00013542,  0.00013662,  0.00013902,  0.00013621,  0.00014   ,\n",
       "        0.00013925,  0.00014087,  0.00014157,  0.00013546,  0.00013743,\n",
       "        0.00013909,  0.00013906,  0.00013853,  0.00013608,  0.00013318,\n",
       "        0.        ,  0.00013463,  0.00013713,  0.0001352 ,  0.00013407,\n",
       "        0.00013423,  0.        ,  0.00013471,  0.00013314,  0.00013329,\n",
       "        0.00013226,  0.00013001,  0.00013102,  0.00013738,  0.00013912,\n",
       "        0.00013697,  0.00013755,  0.00013503,  0.00013451,  0.00013295,\n",
       "        0.00013113,  0.00013256,  0.00013157,  0.00013005,  0.00013141,\n",
       "        0.00012904,  0.00012734,  0.        ,  0.00012561,  0.00012525,\n",
       "        0.00012197,  0.00011729,  0.00011878,  0.00011778,  0.0001157 ,\n",
       "        0.000119  ,  0.00011541,  0.00011476,  0.00011768,  0.00012029,\n",
       "        0.00012033,  0.00012011,  0.00012161,  0.00012116,  0.        ,\n",
       "        0.00012115,  0.00012221,  0.00012141,  0.00012284,  0.00012077,\n",
       "        0.00012051,  0.00011927,  0.00011821,  0.0001154 ,  0.00011653,\n",
       "        0.00011674,  0.00011975,  0.00011769,  0.00012659,  0.00012815,\n",
       "        0.00013406,  0.00013121,  0.00012929,  0.        ,  0.00012848,\n",
       "        0.00012748,  0.00012705,  0.        ,  0.00012544,  0.00012635,\n",
       "        0.00012677,  0.        ,  0.        ,  0.00012627,  0.00012902,\n",
       "        0.00012946,  0.00012979,  0.0001306 ,  0.00013157,  0.00013195,\n",
       "        0.00012914,  0.00012961,  0.00012955,  0.00012951,  0.00013797,\n",
       "        0.00013782,  0.00013695,  0.00013735,  0.00013839,  0.0001396 ,\n",
       "        0.00013582,  0.00013459,  0.00013196,  0.00013122,  0.00013243,\n",
       "        0.00012901,  0.00012782,  0.00012577,  0.00012508,  0.00012369,\n",
       "        0.00012571,  0.00012842,  0.00013083,  0.00012864,  0.        ,\n",
       "        0.00012979,  0.00012708,  0.00012684,  0.00012601,  0.00013035,\n",
       "        0.00012923,  0.00012869,  0.00012598,  0.00012413,  0.00012378,\n",
       "        0.00012219,  0.00012447,  0.00012217,  0.00012084,  0.00012214,\n",
       "        0.00012098,  0.00012061,  0.00012078,  0.00011859,  0.00011908,\n",
       "        0.0001108 ,  0.00010973,  0.00010889,  0.00010984,  0.00011433,\n",
       "        0.00011255,  0.00010768,  0.00010752,  0.00010741,  0.00010575,\n",
       "        0.0001055 ,  0.00010611,  0.00010646,  0.00010731,  0.00011182,\n",
       "        0.00011116,  0.00010956,  0.0001079 ,  0.00010486,  0.00010633,\n",
       "        0.00010593,  0.00010768,  0.00011057,  0.00011016,  0.00011216])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_items_scores"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 根据用户对item的预测打分产生top推荐"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#Sort the indices of user_item_scores based upon their value\n",
    "#Also maintain the corresponding score\n",
    "sort_index = sorted(((e,i) for i,e in enumerate(list(user_items_scores))), reverse=True)\n",
    "    \n",
    "#Create a dataframe from the following\n",
    "columns = ['user_id', 'item', 'score', 'rank']\n",
    "df = pd.DataFrame(columns=columns)\n",
    "         \n",
    "#Fill the dataframe with top 20 item based recommendations\n",
    "rank = 1 \n",
    "for i in range(0,len(sort_index)):\n",
    "    cur_item_index = sort_index[i][1] \n",
    "    cur_item = list (item_index.keys()) [list (item_index.values()).index (cur_item_index)]\n",
    "            \n",
    "    if ~np.isnan(sort_index[i][0]) and cur_item_index not in cur_user_items and rank <= 20:\n",
    "        df.loc[len(df)]=[cur_user,cur_item,sort_index[i][0],rank]\n",
    "        rank = rank+1       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>user_id</th>\n",
       "      <th>item</th>\n",
       "      <th>score</th>\n",
       "      <th>rank</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOBONKR12A58A7A7E0</td>\n",
       "      <td>0.015286</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAUWYT12A81C206F1</td>\n",
       "      <td>0.011241</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOSXLTC12AF72A7F54</td>\n",
       "      <td>0.009074</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOEGIYH12A6D4FC0E3</td>\n",
       "      <td>0.007092</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOFRQTD12A81C233C0</td>\n",
       "      <td>0.006822</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOAXGDH12A8C13F8A1</td>\n",
       "      <td>0.006078</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUFTBI12AB0183F65</td>\n",
       "      <td>0.005908</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOVDSJC12A58A7A271</td>\n",
       "      <td>0.005869</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOPUCYA12A8C13A694</td>\n",
       "      <td>0.005793</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOOFYTN12A6D4F9B35</td>\n",
       "      <td>0.005721</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOHTKMO12AB01843B0</td>\n",
       "      <td>0.005641</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOBOUPA12A6D4F81F1</td>\n",
       "      <td>0.005542</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SONYKOW12AB01849C9</td>\n",
       "      <td>0.005471</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SODJWHY12A8C142CCE</td>\n",
       "      <td>0.005039</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOLFXKT12AB017E3E0</td>\n",
       "      <td>0.004616</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOFLJQZ12A6D4FADA6</td>\n",
       "      <td>0.004217</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOTWNDJ12A8C143984</td>\n",
       "      <td>0.004104</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUNZHU12A8AE47481</td>\n",
       "      <td>0.004086</td>\n",
       "      <td>18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUVTSM12AC468F6A7</td>\n",
       "      <td>0.003798</td>\n",
       "      <td>19</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>5a905f000fc1ff3df7ca807d57edb608863db05d</td>\n",
       "      <td>SOUDLVN12AAFF43658</td>\n",
       "      <td>0.003615</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     user_id                item     score  \\\n",
       "0   5a905f000fc1ff3df7ca807d57edb608863db05d  SOBONKR12A58A7A7E0  0.015286   \n",
       "1   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAUWYT12A81C206F1  0.011241   \n",
       "2   5a905f000fc1ff3df7ca807d57edb608863db05d  SOSXLTC12AF72A7F54  0.009074   \n",
       "3   5a905f000fc1ff3df7ca807d57edb608863db05d  SOEGIYH12A6D4FC0E3  0.007092   \n",
       "4   5a905f000fc1ff3df7ca807d57edb608863db05d  SOFRQTD12A81C233C0  0.006822   \n",
       "5   5a905f000fc1ff3df7ca807d57edb608863db05d  SOAXGDH12A8C13F8A1  0.006078   \n",
       "6   5a905f000fc1ff3df7ca807d57edb608863db05d  SOUFTBI12AB0183F65  0.005908   \n",
       "7   5a905f000fc1ff3df7ca807d57edb608863db05d  SOVDSJC12A58A7A271  0.005869   \n",
       "8   5a905f000fc1ff3df7ca807d57edb608863db05d  SOPUCYA12A8C13A694  0.005793   \n",
       "9   5a905f000fc1ff3df7ca807d57edb608863db05d  SOOFYTN12A6D4F9B35  0.005721   \n",
       "10  5a905f000fc1ff3df7ca807d57edb608863db05d  SOHTKMO12AB01843B0  0.005641   \n",
       "11  5a905f000fc1ff3df7ca807d57edb608863db05d  SOBOUPA12A6D4F81F1  0.005542   \n",
       "12  5a905f000fc1ff3df7ca807d57edb608863db05d  SONYKOW12AB01849C9  0.005471   \n",
       "13  5a905f000fc1ff3df7ca807d57edb608863db05d  SODJWHY12A8C142CCE  0.005039   \n",
       "14  5a905f000fc1ff3df7ca807d57edb608863db05d  SOLFXKT12AB017E3E0  0.004616   \n",
       "15  5a905f000fc1ff3df7ca807d57edb608863db05d  SOFLJQZ12A6D4FADA6  0.004217   \n",
       "16  5a905f000fc1ff3df7ca807d57edb608863db05d  SOTWNDJ12A8C143984  0.004104   \n",
       "17  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUNZHU12A8AE47481  0.004086   \n",
       "18  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUVTSM12AC468F6A7  0.003798   \n",
       "19  5a905f000fc1ff3df7ca807d57edb608863db05d  SOUDLVN12AAFF43658  0.003615   \n",
       "\n",
       "   rank  \n",
       "0     1  \n",
       "1     2  \n",
       "2     3  \n",
       "3     4  \n",
       "4     5  \n",
       "5     6  \n",
       "6     7  \n",
       "7     8  \n",
       "8     9  \n",
       "9    10  \n",
       "10   11  \n",
       "11   12  \n",
       "12   13  \n",
       "13   14  \n",
       "14   15  \n",
       "15   16  \n",
       "16   17  \n",
       "17   18  \n",
       "18   19  \n",
       "19   20  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
